capture log close
clear all
set maxvar 10000
=set more off
pause off
# delimit;

local log "";
local crime "";
local credit "";
local data "";

log using "`log'\crime_prwora_2compile.log", replace;

/* 
Main Prep 2
*/


/* PO/drug/property breakout variables */
local abbr_all "bk" ;
local abbr_non_income 	"nobk" ;
local abbr_vice "vice" ;

foreach type in all non_income vice { ;
	use "`crime'\reference\reference_event_`type'_breakout.dta", clear ;
	foreach var of varlist _all { ;
		rename `var' `abbr_`type''_`var' ;
	} ;
	rename `abbr_`type''_cjars_id cjars_id ;
	tempfile `abbr_`type'' ;
	save ``abbr_`type''' ;
} ;


/* Merge in the linked crime records */
use "`crime'\831\crime_prwora_formerge_prep.dta", clear ;

duplicates drop hun, force;
merge 1:m hun using "`crime'\prep1\crime_prwora_merged.dta", keepusing(part cjars_id);
gen byte indiv_match=(_merge==3);
drop if _merge==2;
drop _merge;
count;

tab indiv_match part, m;
pause ;

/* ********************************************************************************* */
/* **** ELIMINATE DUPLICATES **** */
/* ********************************************************************************* */

duplicates report hun;
duplicates report hun part;

tab part, m ;
forval i = 1(1)2 {;
	gen byte temp`i' = (part == `i') ;
	egen part`i' = max(temp`i'), by(hun) ;
	tab part`i', m ;
};
drop temp* ;

drop if part1==1 & part!=1 ;
drop if part1==0 & part2==1 & part!=2 ;
tab part, m ;
drop part1 part2 ;

duplicates report hun ;
duplicates report hun part ;

sort hun ;
duplicates report hun ;
duplicates drop hun, force ;

tab indiv_match, m ;
pause ;
	
/* ********************************************* */
/* **** MERGE IN EVENTS FROM REFERENCE FILE **** */
/* ********************************************* */

/* Merge reference file */
merge m:1 cjars_id using "`crime'\reference\reference_event.dta" ;
drop if _merge == 2 ;
drop _merge ;

/* Merge reference file for breakouts */
foreach type in bk nobk vice { ;
	merge m:1 cjars_id using ``type'' ;
	drop if _merge == 2 ;
	drop _merge ;
} ;

/* Merge incarceration spells */
foreach type in hybrid_incarceration incarceration parole probation hybrid_jail { ;
	merge m:1 cjars_id using "`crime'\reference\\`type'_annual_1997_2019_flat.dta", keepusing(days_in_*) ;
	drop if _merge == 2 ;
	drop _merge ;
	if "`type'" == "hybrid_incarceration" { ;
		forval yr = 1997(1)2019 { ;
			capture rename days_in_prison`yr' days_in_hprison`yr' ;
		} ;
	} ;
} ;

foreach type in hybrid_incarceration hybrid_jail parole probation { ;
	merge m:1 cjars_id using "`crime'\reference\\`type'_byage_18_40_flat.dta", keepusing(days_in_*) ;
	drop if _merge == 2 ;
	drop _merge ;
	if "`type'" == "hybrid_incarceration" { ;
		forval age = 18(1)40 { ;
			capture rename days_in_prison`age' days_in_hprison`age' ;
		} ;
	} ;
} ;

foreach var of varlist days_in_* { ;
	replace `var' = 0 if `var' == . ;
} ;

/* Save data set */
save "`crime'\prep2\crime_prwora_remerge.dta", replace;

capture log close;
